
package riccsson.core.xnatest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.view.ViewGroup.LayoutParams;
import android.util.Log; 
import android.content.res.AssetManager;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import java.io.*;

public class xnatest extends Activity implements SurfaceHolder.Callback
{
    private static String TAG = "EglSample";
	private AssetManager m_assetManager;

 
    @Override public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
 
        Log.i(TAG, "onCreate()");
 
        //setContentView(R.layout.main);
        //SurfaceView surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
		
		SurfaceView surfaceView = new SurfaceView(this);
		surfaceView.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

		setContentView(surfaceView);
        surfaceView.getHolder().addCallback(this);
        surfaceView.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				//Toast toast = Toast.makeText(NativeEglExample.this,
				//	 "This demo combines Java UI and native EGL + OpenGL renderer",
				//	 Toast.LENGTH_LONG);
				//toast.show();
			}
		});
		
		surfaceView.setOnTouchListener(new OnTouchListener()
		{
			public boolean onTouch(View v, MotionEvent m)
			{
				handleTouch(m);     				
				return true;
			}
		});

    }
 
	void handleTouch(MotionEvent m)
	{
    	int pointerCount = m.getPointerCount();
    	
    	for (int i = 0; i < pointerCount; i++)
    	{
    		float x = m.getX(i);
    		float y = m.getY(i);    		
    		int id = m.getPointerId(i);
    		int action = m.getActionMasked();
    		int actionIndex = m.getActionIndex();
    		String actionString;
    		
    		switch (action)
    		{
    			case MotionEvent.ACTION_DOWN:
    				actionString = "DOWN";
    				break;
    			case MotionEvent.ACTION_UP:
    				actionString = "UP";
    				break;	
    			case MotionEvent.ACTION_POINTER_DOWN:
    				actionString = "PNTR DOWN";
    				break;
    			case MotionEvent.ACTION_POINTER_UP:
        			actionString = "PNTR UP";
        			break;
    			case MotionEvent.ACTION_MOVE:
    				actionString = "MOVE";
    				break;
    			default:
    				actionString = "";
    		}
			
			nativeOnTouch(i, x, y, action);
    	}
	}

    @Override protected void onStart()
	{
        super.onStart();
        Log.i(TAG, "onStart()");
        //nativeOnStart();
    }
 
    @Override protected void onResume()
	{
        super.onResume();
        Log.i(TAG, "onResume()");
        nativeOnResume();
    }
 
    @Override protected void onPause()
	{
        super.onPause();
        Log.i(TAG, "onPause()");
        nativeOnPause();
    }
 
    @Override protected void onStop()
	{
        super.onStop();
        Log.i(TAG, "onStop()");
        nativeOnStop();
    }
 
    public void surfaceChanged(final SurfaceHolder holder, int format, int w, int h)
	{
		// Get asset manager.
		m_assetManager = this.getAssets();
		
		this.runOnUiThread(new Runnable()
		{
			@Override public void run()
			{
				nativeSetSurface(holder.getSurface());
				nativeOnStart(m_assetManager);
			}
		});
    }
 
    public void surfaceCreated(SurfaceHolder holder)
	{
    }
 
    public void surfaceDestroyed(SurfaceHolder holder)
	{
        nativeSetSurface(null);
    }
	
    public static native void nativeOnStart(AssetManager assetManager);
    public static native void nativeOnResume();
    public static native void nativeOnPause();
    public static native void nativeOnStop();
    public static native void nativeSetSurface(Surface surface);
    public static native void nativeOnTouch(int touchId, float x, float y, int state);
	
	static
	{
		System.loadLibrary("RiccssonXNATest_android");
	}
}